package mvp.com.bbc.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

import mvp.com.bbc.R;
import mvp.com.bbc.appContact.AppContact;
import mvp.com.bbc.bean.ArtBeans;
import mvp.com.bbc.di.component.DaggerArtListComponent;
import mvp.com.bbc.di.modules.ArtListModules;
import mvp.com.bbc.ui.presenter.ArtListPresenter;
import mvp.com.bbc.ui.witget.PullToRefreshLayout;
import mvp.com.bbc.ui.witget.PullableRecyclerViewClever;

/**
 * Created by Administrator on 2017/9/29.
 */

public class IMFragment extends Fragment implements AppContact.ArtListView {

    private View mInflate;
    private PullToRefreshLayout mPullToRefreshLayout;
    private PullableRecyclerViewClever mRv;
    private ArtListPresenter mPresenter;
    private ImageView empty;
    private int page = 1;
    private ArtListAdapter mAdapter;
    private String type = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mInflate = inflater.inflate(R.layout.fragment_community_list, container, false);
        return mInflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mPullToRefreshLayout = (PullToRefreshLayout) mInflate.findViewById(R.id.pullToRefreshLayout);
        mRv = (PullableRecyclerViewClever) mInflate.findViewById(R.id.community_content_view);
        empty = (ImageView) mInflate.findViewById(R.id.iv_visible_empty);
        mPresenter = DaggerArtListComponent
                .builder()
                .artListModules(new ArtListModules(this))
                .build()
                .getPresenter();
        mPresenter.artListPresenter("1", page + "", "80");
        mRv.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new ArtListAdapter(getContext(), type);
        mRv.setAdapter(mAdapter);
        mRv.setRefresh(true);
        mRv.setLoad(true);
        mPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                mPresenter.artListPresenter("1", page + "", "80");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPullToRefreshLayout.loadmoreFinish(pullToRefreshLayout.SUCCEED);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                page += 1;
                mPresenter.artListPresenter("1", page + "", "80");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPullToRefreshLayout.loadmoreFinish(pullToRefreshLayout.SUCCEED);
                    }
                }, 1000);
            }
        });
    }

    @Override
    public void artListView(ArtBeans artBeans) {
        List<ArtBeans.ArtBean> art = artBeans.getArt();
        if (art != null && art.size() > 0) {
            if (page == 1) {
                mAdapter.initData(art);
            } else {
                mAdapter.addData(art);
            }
        } else {
            empty.setVisibility(View.VISIBLE);
        }
    }
}
